package com.wu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.mapper.ArticleDraftMapper;
import com.wu.pojo.ArticleDraft;
import com.wu.pojo.model.UserToken;
import com.wu.service.ArticleDraftService;
import com.wu.util.ArticleConversionUtil;
import com.wu.util.MyUtil;
import com.wu.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wu.constant.Constant.ARTICLE;
import static com.wu.constant.Constant.ARTICLES;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-09 08:53
 */
@Service
public class ArticleDraftServiceImpl extends ServiceImpl<ArticleDraftMapper, ArticleDraft> implements ArticleDraftService {

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private ArticleConversionUtil articleConversionUtil;

    @Override
    public ArticleDraft insertArticleDraft(String articleTitle, String articleContent) {
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        String detailedTime = myUtil.getCurrentDetailedTime();
        String id = IdWorker.get32UUID();
        ArticleDraft draft = new ArticleDraft();
        draft.setArticleId(id);
        draft.setUserId(userId);
        draft.setArticleTitle(articleTitle);
        draft.setArticleContent(articleContent);
        draft.setArticleDate(detailedTime);
        Integer integer = baseMapper.insertArticleDraft(draft);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "保存草稿失败：ArticleDraftServiceImpl，57行");
        }
        return draft;
    }

    @Override
    public Map<String, Object> getArticleDraftListByUserId() {
        Map<String, Object> map = new HashMap<>();
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        List<ArticleDraft> articleDrafts = baseMapper.selectList(Wrappers.<ArticleDraft>query().eq("user_id", userId));
        List<ArticleDraft> articleDraftConversion = articleConversionUtil.getArticleDraftConversion(articleDrafts);
        map.put(ARTICLES, articleDraftConversion);
        return map;
    }

    @Override
    public void deleteArticleDraftByArticleId(String articleId) {
        int integer = baseMapper.deleteById(articleId);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "删除失败：ArticleDraftServiceImpl，77行");
        }
    }

    @Override
    public ArticleDraft updateArticleDraft(String articleId, String articleTitle, String articleContent) {
        String detailedTime = myUtil.getCurrentDetailedTime();
        Integer integer = baseMapper.updateArticleDraft(articleId, articleTitle, articleContent, detailedTime);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "删除失败：ArticleDraftServiceImpl，85行");
        }
        ArticleDraft articleDraft = new ArticleDraft();
        articleDraft.setArticleId(articleId);
        articleDraft.setArticleTitle(articleTitle);
        articleDraft.setArticleContent(articleContent);
        articleDraft.setArticleDate(detailedTime);
        return articleDraft;
    }

    @Override
    public Map<String, Object> getArticleDraftByArticleId(String articleId) {
        ArticleDraft draft = baseMapper.selectById(articleId);
        Map<String, Object> map = new HashMap<>();
        map.put(ARTICLE, draft);
        return map;
    }
}
